﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace JsonToSql
{
    /// <summary>
    /// Request in json format
    /// </summary>
    public class JsonRequest
    {
        private JObject jObject;
        /// <summary>
        /// Requested string
        /// </summary>
        /// <param name="json">string in json format</param>
        public JsonRequest(string json)
        {
            jObject = JObject.Parse(json);
            JToken token=jObject.SelectToken("#Entities");
            if (token != null)
            {
                entities = (from item in token.Children().ToList()
                            select item.ToObject<JsonEntity>()).ToDictionary(x => x.Name, x => x);
            }
            tables = new ReadOnlyCollection<JsonTable>((from item in jObject.Children().ToList()
                                                        where item.Path.StartsWith("#+", StringComparison.OrdinalIgnoreCase)
                                                        select new JsonTable(item, this)
                                 ).ToList());

            tablesToDelete = new ReadOnlyCollection<JsonTableDelete>((from item in jObject.Children().ToList()
                                                        where item.Path.StartsWith("#-", StringComparison.OrdinalIgnoreCase)
                                                        select new JsonTableDelete(item, this)
                               ).ToList());

            tablesToResult = new ReadOnlyCollection<JsonTableResult>((from item in jObject.Children().ToList()
                                                                      where item.Path.StartsWith("#?", StringComparison.OrdinalIgnoreCase)
                                                                      select new JsonTableResult(item, this)
                              ).ToList());
        }
        private Dictionary<string, JsonEntity> entities;
        /// <summary>
        /// Entities by the name
        /// </summary>
        public Dictionary<string, JsonEntity> Entities
        {
            get
            {
                
                return entities;
            }
        }
        private ReadOnlyCollection<JsonTable> tables;
        /// <summary>
        /// All tables
        /// </summary>
        public ReadOnlyCollection<JsonTable> Tables
        {
            get
            {
               
                return tables;
            }
        }

        private ReadOnlyCollection<JsonTableDelete> tablesToDelete;
        /// <summary>
        /// All tables for deletion
        /// </summary>
        public ReadOnlyCollection<JsonTableDelete> TablesToDelete
        {
            get
            {

                return tablesToDelete;
            }
        }

        private ReadOnlyCollection<JsonTableResult> tablesToResult;
        /// <summary>
        /// All tables for deletion
        /// </summary>
        public ReadOnlyCollection<JsonTableResult> TablesToResult
        {
            get
            {

                return tablesToResult;
            }
        }
    }
}
